package com.cc.openeye.dynamic

import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import com.cc.openeye.base.BasePager
import com.cc.openeye.dynamic.follow.FollowComponent
import com.cc.openeye.dynamic.recommend.RecommendComponent
import com.cc.openeye.dynamic.recommend.RecommendViewModel
import com.cc.openeye.height
import com.cc.openeye.padding
import com.cc.openeye.width
import com.tencent.kuikly.compose.foundation.layout.Column
import com.tencent.kuikly.compose.foundation.layout.fillMaxHeight
import com.tencent.kuikly.compose.foundation.layout.fillMaxWidth
import com.tencent.kuikly.compose.foundation.pager.HorizontalPager
import com.tencent.kuikly.compose.foundation.pager.rememberPagerState
import com.tencent.kuikly.compose.material3.HorizontalDivider
import com.tencent.kuikly.compose.material3.Tab
import com.tencent.kuikly.compose.material3.TabRow
import com.tencent.kuikly.compose.material3.TabRowDefaults
import com.tencent.kuikly.compose.material3.TabRowDefaults.tabIndicatorOffset
import com.tencent.kuikly.compose.material3.Text
import com.tencent.kuikly.compose.setContent
import com.tencent.kuikly.compose.ui.Alignment
import com.tencent.kuikly.compose.ui.Modifier
import com.tencent.kuikly.compose.ui.graphics.Color
import com.tencent.kuikly.compose.ui.text.style.TextAlign
import com.tencent.kuikly.compose.ui.unit.dp
import com.tencent.kuikly.core.annotations.Page
import com.tencent.kuikly.core.coroutines.launch


@Page("dynamicPage", supportInLocal = true)
class DynamicPage : BasePager() {


    override fun willInit() {
        super.willInit()
        setContent {
            Column(
                modifier = Modifier.padding(top = pageData.statusBarHeight).fillMaxWidth(),
                horizontalAlignment = Alignment.CenterHorizontally,
            ) {
                TabViewPage()
            }
        }
    }


    @Composable
    private fun TabViewPage() {

        val dynamicViewModel by lazy {
            RecommendViewModel.newInstance()
        }

        val tabDataList = listOf("推荐", "关注")
        val pagerState = rememberPagerState {
            2
        }

        val rememberCoroutineScope = rememberCoroutineScope()

        TabRow(
            0,
            modifier = Modifier.width(150f),
            indicator = { tabPositions ->
                if (pagerState.currentPage < tabDataList.size) {
                    TabRowDefaults.PrimaryIndicator(
                        Modifier.tabIndicatorOffset(tabPositions[pagerState.currentPage]),
                        width = 10.dp,
                        color = Color(0xff333333)
                    )
                }
            },
            divider = {
                HorizontalDivider(modifier = Modifier.height(0f))
            }
        ) {
            tabDataList.forEachIndexed { index, s ->
                Tab(
                    pagerState.currentPage == index,
                    onClick = {
                        lifecycleScope.launch {
                            pagerState.animateScrollToPage(index)
                        }
                    })
                {
                    Text(
                        s,
                        color = Color(if (pagerState.currentPage == index) 0xff333333 else 0xffaaaaaa),
                        modifier = Modifier.padding(top = 10f, bottom = 10f),
                        textAlign = TextAlign.Center
                    )
                }
            }
        }

        HorizontalPager(
            pagerState,
            beyondViewportPageCount = 2,
            modifier = Modifier.fillMaxWidth().fillMaxHeight()
        ) { page ->
            if (page == 0) {
                RecommendComponent()
            } else {
                FollowComponent()
            }
        }
    }

}

